"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 使用gs_guc的方式修改参数，备份再恢复
Create At   : 2020/12/1
@zou_jialiang0505328126
Description :
    1.备份数据
    2.修改参数most_available_sync=on(默认为off)
    3.恢复数据库
    4查询most_available_sync
    5.修改参数most_available_sync=on(默认为off)
    6.备份数据
    7.还原most_available_sync
    8.恢复数据库
    9.查询most_available_sync值
Expect      :
    1.备份成功
    2.修改参数成功
    3.恢复数据库成功
    4.查询most_available_sync参数值为off
    5.修改参数为on成功
    6.备份成功
    7.还原参数为off成功
    8.恢复成功
    9.查询参数值为on
History     :
    modified: 2020/12/10  modified by @wan005,增加对most_available_sync参数默认值检查，若默认值非off先修改为off
"""

import os
import unittest
import sys
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    commonshsta2 = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Tools_Backup_Case038.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbStandby2DbUser = Node(node='Standby2DbUser')
        self.dbStandby1Root = Node(node='Standby1Root')
        self.dbPrimaryRootNode = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.backup_file = os.path.join(macro.DB_BACKUP_PATH, 'backup_dir')
        self.primary_hostname = self.dbPrimaryDbUser.sh('hostname').result()

        self.log.info('-------------------创建备份路径-----------------------------------')
        shell_cmd = f'rm -rf {self.backup_file}; mkdir {self.backup_file};'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('---------------------------------检查most_available_sync默认值-----------------------------')
        self.most_available_sync = self.commonshpri.executeGsguc('check', 'most_available_sync=off', 'most_available_sync', self.primary_hostname)
        if not self.most_available_sync:
            self.log.info('=================设置most_available_sync为off=====================')
            result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=off',
                                                   self.primary_hostname)
            self.assertTrue(result)
            result = self.commonshpri.stopDbCluster()
            self.assertTrue(result)
            result = self.commonshpri.startDbCluster()
            self.assertTrue(result)

    def test_backup(self):
        self.log.info("-----------------------------1.备份数据：成功---------------------------------")
        result = self.commonshpri.execu_gs_backup('backup', self.primary_hostname, self.backup_file, "--all", )
        self.assertIn(self.Constant.BACKUP_BACKUP_SUCCESS_MSG, result)
        cmd = f'ls -l {self.backup_file} |grep "^-"|wc -l'
        self.log.info(cmd)
        result = self.dbPrimaryRootNode.sh(cmd).result()
        self.log.info(result)
        self.assertLess(0, int(result))

        self.log.info('-----------------------------2.修改参数most_available_sync=on(默认为off):成功------------------------------')
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=on',
                                               self.primary_hostname)
        self.assertTrue(result)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.executeGsguc('check', 'most_available_sync=on', 'most_available_sync', self.primary_hostname)
        self.assertTrue(result)

        self.log.info('-------------------3.恢复数据库:成功-----------------------------------')
        result = self.commonshpri.execu_gs_backup('restore', self.primary_hostname, self.backup_file, "--all")
        self.assertIn(self.Constant.BACKUP_RESTORE_SUCCESS_MSG, result)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('-------------------4查询most_available_sync:成功------------------------')
        result = self.commonshpri.executeGsguc('check', 'most_available_sync=on', 'most_available_sync', self.primary_hostname)
        self.assertFalse(result)

        self.log.info(
            '-----------------------------5.修改参数most_available_sync=on(默认为off):成功------------------------------')
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=on',
                                               self.primary_hostname)
        self.assertTrue(result)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.executeGsguc('check', 'most_available_sync=on', 'most_available_sync',
                                               self.primary_hostname)
        self.assertTrue(result)

        self.log.info("-----------------------------6.备份数据：成功---------------------------------")
        result = self.commonshpri.execu_gs_backup('backup', self.primary_hostname, self.backup_file, "--all", )
        self.assertIn(self.Constant.BACKUP_BACKUP_SUCCESS_MSG, result)
        cmd = f'ls -l {self.backup_file} |grep "^-"|wc -l'
        self.log.info(cmd)
        result = self.dbPrimaryRootNode.sh(cmd).result()
        self.log.info(result)
        self.assertLess(0, int(result))

        self.log.info(
            '-----------------------------7.还原most_available_sync:成功------------------------------')
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=off',
                                               self.primary_hostname)
        self.assertTrue(result)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.executeGsguc('check', 'most_available_sync=off', 'most_available_sync',
                                               self.primary_hostname)
        self.assertTrue(result)

        self.log.info('-------------------8.恢复数据库:成功-----------------------------------')
        result = self.commonshpri.execu_gs_backup('restore', self.primary_hostname, self.backup_file, "--all")
        self.assertIn(self.Constant.BACKUP_RESTORE_SUCCESS_MSG, result)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('-------------------9查询most_available_sync:成功------------------------')
        result = self.commonshpri.executeGsguc('check', 'most_available_sync=on', 'most_available_sync',
                                               self.primary_hostname)
        self.assertTrue(result)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------------------删除备份文件------------------')
        shell_cmd = f'rm -rf {self.backup_file}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)
        result = self.dbStandby1DbUser.sh(shell_cmd).result()
        self.log.info(result)
        result = self.dbStandby2DbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----------------------------恢复recovery_max_workers-----------------')
        if self.most_available_sync:
            result = self.commonshpri.executeGsguc('check', 'most_available_sync=on', 'most_available_sync', self.primary_hostname)
            if result:
                result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=off',
                                                       self.primary_hostname)
                self.log.info(result)
                result = self.commonshpri.stopDbCluster()
                self.log.info(result)
                result = self.commonshpri.startDbCluster()
                self.log.info(result)
        else:
            result = self.commonshpri.executeGsguc('check', 'most_available_sync=off', 'most_available_sync',
                                                   self.primary_hostname)
            if result:
                result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG,
                                                       'most_available_sync=on',
                                                       self.primary_hostname)
                self.log.info(result)
                result = self.commonshpri.stopDbCluster()
                self.log.info(result)
                result = self.commonshpri.startDbCluster()
                self.log.info(result)
        self.log.info('----------Opengauss_Reliability_Tools_Backup_Case038.py 执行结束-----------------------')
